/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.tracer.sample.springcloud.controller;

import com.lvyh.lightframe.tracer.plugin.springcloud.FeignClientRequestInterceptor;
import com.lvyh.lightframe.tracer.sample.springcloud.common.CustomUserInfoService;
import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.Target;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Dynamically create feignclient
 */
@Import(FeignClientsConfiguration.class)
@RestController
public class CustomUserMgrController {

    private static final Logger logger = LoggerFactory.getLogger(CustomUserMgrController.class);

    // http://localhost:8083/dynamic-feign-client-send
    @RequestMapping(value = "dynamic-feign-client-send")
    @ResponseBody
    public String invokeTest(HttpServletRequest request, HttpServletResponse response) {
        logger.info("[CustomFeignClientController] invoke execution start");

        //serviceId可从配置中心读取
        String serviceId = "cloud-provider-server";
        Map<String, String> map = new HashMap<>();
        map.put("parameter1", "test1");
        map.put("parameter2", "test2");

        //Note that HTTP must be added to the constructed URL://
        customUserInfoService = this.builder.target(CustomUserInfoService.class, "http://" + serviceId);

        //Using feignclient to request services
        String result = customUserInfoService.createUser(map);

        return "print invoke result:" + result;
    }


    // http://localhost:8083/dynamic-feign-client-send2
    @RequestMapping(value = "dynamic-feign-client-send2")
    @ResponseBody
    public String invokeTest2(HttpServletRequest request, HttpServletResponse response) {
        logger.info("[CustomFeignClientController] Business execution start");

        //Serviceid and URL can be read from the configuration center
        String serviceId = "cloud-provider-server";
        Map<String, String> map = new HashMap<>();
        map.put("parameter1", "test1");
        map.put("parameter2", "test2");

        //Note that HTTP must be added to the constructed URL://
        Target.HardCodedTarget<CustomUserInfoService> hardCodedTarget = new Target.HardCodedTarget(CustomUserInfoService.class, "http://" + serviceId, "http://127.0.0.1:8084");
        customUserInfoService = this.builder.target(hardCodedTarget);

        String result = customUserInfoService.createUser(map);
        return "print invoke result:" + result;
    }

    //Custom feignclient
    CustomUserInfoService customUserInfoService;

    //Feign native constructor
    Feign.Builder builder;

    //Create constructor
    public CustomUserMgrController(Decoder decoder, Encoder encoder, Client client, Contract contract) {
        this.builder = Feign.builder()
                .client(client)
                .requestInterceptor(new FeignClientRequestInterceptor())
                .encoder(encoder)
                .decoder(decoder)
                .contract(contract);
    }

}
